<?php
/* 
 * class.sellerBase.php
 * 商家基础公共类
 * @version 0.1
 * @author samuel
 * @date 2009-05-08
 */
class sellerBase {

    public $core ;
    public $params;
    public $error;

    function  __construct() {
        $this->core = $GLOBALS['CORE'];
        $this->params = $this->core->params;
        $this->error = "";
    }

    //获取商家列表
    function getSellerList($arr_filter = "", $arr_orderby = "", $arr_limit = array('page' => 1, 'pagesize' => 20, 'url' => '')){
		$arr_sql_appends = $this->buildAppendSql($arr_filter, $arr_orderby, $arr_limit);
        $append_where = $arr_sql_appends['where'];
        $append_orderby = $arr_sql_appends['order'];
        $append_limit = $arr_sql_appends['limit'];
        $append_page_where = $arr_sql_appends['page_where'];

        //分页处理
        $pager = $this->core->utility->pager('fun_seller', 'SellerID', $append_page_where, $arr_limit);

		$sql = "SELECT a.`SellerID`, a.`SellerClassID`, a.`SellerNameCN`, a.`SellerType`, c.`SellerCBDNameCN`, d.`SellerCityNameCN`, e.`SellerProvinceNameCN`";
		$sql .= " FROM `fun_seller` as a ";
        //$sql .= "LEFT JOIN `fun_seller_class` as b ON a.`SellerClassID`=b.`SellerClassID` ";
		$sql .= " LEFT JOIN `fun_seller_cbd` as c ON a.`SellerCBDID`=c.`SellerCBDID` ";
		$sql .= " LEFT JOIN `fun_seller_city` as d ON a.`SellerCityID`=d.`SellerCityID` ";
		$sql .= " LEFT JOIN `fun_seller_province` as e ON a.`SellerProvinceID`=e.`SellerProvinceID` ";
		$sql .= " WHERE 1 AND `del`=0 " . $append_where . $append_orderby . $append_limit;

        //echo $sql;

        $arr_sellers = array();

        $sellers = $this->core->db->get_results($sql);
        if(!empty($sellers)){
            foreach($sellers as $seller){
                $classname = $this->getSellerClassName($seller->SellerClassID);
                $arr_sellers[] = array(
                    'SellerID' => $seller->SellerID,
                    'SellerNameCN' => $seller->SellerNameCN,
                    'SellerType' => $seller->SellerType,
					'SellerClassName' => $classname,
					'SellerCBDNameCN' => $seller->SellerCBDNameCN,
					'SellerCityNameCN' => $seller->SellerCityNameCN,
					'SellerProvinceNameCN' => $seller->SellerProvinceNameCN,
                );
            }
        }

        return array('list' => $arr_sellers, 'pager' => $pager);
    }

    function getSellerClassName($ids){
        if(!$ids) return;
        $arr_ids = explode(',', $ids);
        $arr_classname = array();
        foreach($arr_ids as $id){
            if(!$id) continue ;
            $sql = "SELECT `SellerClassName` FROM `fun_seller_class` WHERE `SellerClassID`=".$id;
            $arr_classname[] = $this->core->db->get_var($sql);
        }
        $str_classname = implode(',', $arr_classname);
        return $str_classname;
    }

    //获取商家分类
    function getSellerCate(){
        $arr_sellercates = array();
        $sql = "SELECT `SellerClassID`, `SellerClassName`, `SellerClassOwner`, `SellerCreateMoney`, `SellerPicMoney`, `SellerGradeMoney`, `SellerMsgMoney` FROM `fun_seller_class`";

        $cates = $this->core->db->get_results($sql);
        if(!$cates) return false;
        foreach($cates as $cate){
            $arr_sellercates[] = array(
                'SellerClassID' => $cate->SellerClassID,
                'SellerClassName' => $cate->SellerClassName,
                'SellerClassOwner' => $cate->SellerClassOwner,
                'SellerCreateMoney' => $cate->SellerCreateMoney,
                'SellerGradeMoney' => $cate->SellerGradeMoney,
                'SellerPicMoney' => $cate->SellerPicMoney,
                'SellerMsgMoney' => $cate->SellerMsgMoney
            );
        }
        return $arr_sellercates;
    }

    //获取商圈
    function getSellerCBD(){
        $arr_sellerCBDs = array();
        $sql = "SELECT `SellerCBDID`, `SellerCBDNameCN`, `SellerCityID` FROM `fun_seller_cbd`";
        $CDBs = $this->core->db->get_results($sql);
        if(!$CDBs) return false;
        foreach($CDBs as $CDB){
            $arr_sellerCBDs[] = array(
                'SellerCBDID' => $cate->SellerCBDID,
                'SellerCBDNameCN' => $cate->SellerCBDNameCN,
                'SellerCityID' => $cate->SellerCityID
            );
        }
        return $arr_sellerCBDs;
    }

    //构建SQL查询语句
    function buildAppendSql($arr_filter = "", $arr_orderby = "", $arr_limit = array('page' => 1, 'pagesize' => 20, 'url' => '')){
        $append_where = "";
		$append_orderby = "";
		$append_limit = "";
        $append_page_where = "";

		if(!empty($arr_filter)){
			foreach($arr_filter as $filter){
                $join_append = "a";
                if($filter['join']) $join_append = $filter['join'];
				if($filter['match'] != 'like'){
					$append_where .= " AND ".$join_append.".`" . $filter['attr'] . "` = '" . $filter['value'] . "'";
                    if($join_append == "a") $append_page_where .= " AND ".$join_append.".`" . $filter['attr'] . "` = '" . $filter['value'] . "'";
				}else{
					$append_where .= " AND ".$join_append.".`" . $filter['attr'] . "` LIKE '%" . $filter['value'] . "%'";
                    if($join_append == "a") $append_page_where .= " AND ".$join_append.".`" . $filter['attr'] . "` LIKE '%" . $filter['value'] . "%'";
				}
			}
		}

		if(!empty($arr_orderby)){
			foreach($arr_orderby as $attr => $orderby){
				$append_orderby .= " ORDER BY a.`" . $attr . "` " . $orderby;
			}
		}

		if(!empty($arr_limit)){
            $arr_limit['offset'] = ($arr_limit['page'] - 1) * $arr_limit['pagesize'];
            $append_limit .= " LIMIT " . $arr_limit['offset'] . "," . $arr_limit['pagesize'];
        }

        return array('where' => $append_where, 'page_where' => $append_page_where, 'order' => $append_orderby, 'limit' => $append_limit);
    }

    //获取省份列表
    function getSellerProvinceList($arr_filter = "", $arr_orderby = "", $arr_limit = array('page' => 1, 'pagesize' => 20, 'url' => '')){
        $arr_sql_appends = $this->buildAppendSql($arr_filter, $arr_orderby, $arr_limit);
        $append_where = $arr_sql_appends['where'];
        $append_orderby = $arr_sql_appends['order'];
        $append_limit = $arr_sql_appends['limit'];
        $append_page_where = $arr_sql_appends['page_where'];

        //分页处理
        $pager = $this->core->utility->pager('fun_seller_province', 'SellerProvinceID', $append_page_where, $arr_limit);


        $sql = "SELECT a.`SellerProvinceID`, a.`SellerProvinceChar`, a.`SellerProvinceNameCN`";
		$sql .= " FROM `fun_seller_province` as a ";
		$sql .= " WHERE 1" . $append_where . $append_orderby . $append_limit;
        //echo $sql;

        $arr_sellerProvinces = array();
        
        $provinces = $this->core->db->get_results($sql);
        if(!empty($provinces)){
            foreach($provinces as $province){
                $arr_sellerProvinces[] = array(
                    'SellerProvinceID' => $province->SellerProvinceID,
                    'SellerProvinceChar' => $province->SellerProvinceChar,
                    'SellerProvinceNameCN' => $province->SellerProvinceNameCN
                );
            }
        }
        
        return array('list' => $arr_sellerProvinces, 'pager' => $pager);
    }

	 //获取城市列表
    function getSellerCityList($arr_filter = "", $arr_orderby = "", $arr_limit = array('page' => 1, 'pagesize' => 20, 'url' => '')){
        $arr_sql_appends = $this->buildAppendSql($arr_filter, $arr_orderby, $arr_limit);
        $append_where = $arr_sql_appends['where'];
        $append_orderby = $arr_sql_appends['order'];
        $append_limit = $arr_sql_appends['limit'];
        $append_page_where = $arr_sql_appends['page_where'];

        //分页处理
        $pager = $this->core->utility->pager('fun_seller_city', 'SellerCityID', $append_page_where, $arr_limit);


        $sql = "SELECT a.`SellerCityID`, a.`SellerProvinceID`, a.`SellerCityNameCN`, a.`SellerCityNo`, b.`SellerProvinceNameCN`";
		$sql .= " FROM `fun_seller_city` as a ";
		$sql .= " LEFT JOIN `fun_seller_province` as b ON a.`SellerProvinceID`=b.`SellerProvinceID`";
		$sql .= " WHERE 1" . $append_where . $append_orderby . $append_limit;
        //echo $sql;

        $arr_sellerCities = array();

        $cities = $this->core->db->get_results($sql);
        if(!empty($cities)){
            foreach($cities as $city){
                $arr_sellerCities[] = array(
                    'SellerCityID' => $city->SellerCityID,
                    'SellerCityNameCN' => $city->SellerCityNameCN,
                    'SellerCityNo' => $city->SellerCityNo,
					'SellerProvinceNameCN' => $city->SellerProvinceNameCN
                );
            }
        }

        return array('list' => $arr_sellerCities, 'pager' => $pager);
    }

    //获取商圈列表
    function getSellerCBDList($arr_filter = "", $arr_orderby = "", $arr_limit = array('page' => 1, 'pagesize' => 20, 'url' => '')){
        $arr_sql_appends = $this->buildAppendSql($arr_filter, $arr_orderby, $arr_limit);
        $append_where = $arr_sql_appends['where'];
        $append_orderby = $arr_sql_appends['order'];
        $append_limit = $arr_sql_appends['limit'];
        $append_page_where = $arr_sql_appends['page_where'];

        //分页处理
        $pager = $this->core->utility->pager('fun_seller_cbd', 'SellerCBDID', $append_page_where, $arr_limit);


        $sql = "SELECT a.`SellerCBDID`, a.`SellerCBDNameCN`, b.`SellerCityNameCN`";
		$sql .= " FROM `fun_seller_cbd` as a ";
		$sql .= " LEFT JOIN `fun_seller_city` as b ON a.`SellerCityID`=b.`SellerCityID`";
		$sql .= " WHERE 1" . $append_where . $append_orderby . $append_limit;
        //echo $sql;

        $arr_sellerCBDs = array();

        $cbds = $this->core->db->get_results($sql);
        if(!empty($cbds)){
            foreach($cbds as $cbd){
                $arr_sellerCBDs[] = array(
                    'SellerCBDID' => $cbd->SellerCBDID,
                    'SellerCBDNameCN' => $cbd->SellerCBDNameCN,
                    'SellerCityNameCN' => $cbd->SellerCityNameCN
                );
            }
        }

        return array('list' => $arr_sellerCBDs, 'pager' => $pager);
    }

    //获取地区列表
    function getSellerAreaList($arr_filter = "", $arr_orderby = "", $arr_limit = array('page' => 1, 'pagesize' => 20, 'url' => '')){
        $arr_sql_appends = $this->buildAppendSql($arr_filter, $arr_orderby, $arr_limit);
        $append_where = $arr_sql_appends['where'];
        $append_orderby = $arr_sql_appends['order'];
        $append_limit = $arr_sql_appends['limit'];
        $append_page_where = $arr_sql_appends['page_where'];

        //分页处理
        $pager = $this->core->utility->pager('fun_seller_area', 'SellerAreaID', $append_page_where, $arr_limit);


        $sql = "SELECT a.`SellerAreaID`, a.`SellerAreaNameCN`, b.`SellerCityNameCN`";
		$sql .= " FROM `fun_seller_area` as a ";
		$sql .= " LEFT JOIN `fun_seller_city` as b ON a.`SellerCityID`=b.`SellerCityID`";
		$sql .= " WHERE 1" . $append_where . $append_orderby . $append_limit;
        //echo $sql;

        $arr_sellerAreas = array();

        $areas = $this->core->db->get_results($sql);
        if(!empty($areas)){
            foreach($areas as $area){
                $arr_sellerAreas[] = array(
                    'SellerAreaID' => $area->SellerAreaID,
                    'SellerAreaNameCN' => $area->SellerAreaNameCN,
                    'SellerCityNameCN' => $area->SellerCityNameCN
                );
            }
        }

        return array('list' => $arr_sellerAreas, 'pager' => $pager);
    }

	//获取折扣列表
    function getSellerDiscountList($arr_filter = "", $arr_orderby = "", $arr_limit = array('page' => 1, 'pagesize' => 20, 'url' => '')){
        $arr_sql_appends = $this->buildAppendSql($arr_filter, $arr_orderby, $arr_limit);
        $append_where = $arr_sql_appends['where'];
        $append_orderby = $arr_sql_appends['order'];
        $append_limit = $arr_sql_appends['limit'];
        $append_page_where = $arr_sql_appends['page_where'];

        //分页处理
        $pager = $this->core->utility->pager('fun_seller_discount', 'SellerDisID', $append_page_where, $arr_limit);


        $sql = "SELECT a.`SellerDisID`, a.`SellerMsg`, a.`SellerMsgIs`, a.`SellerMsgTime`, b.`SellerNameCN`";
		$sql .= " FROM `fun_seller_discount` as a ";
		$sql .= " LEFT JOIN `fun_seller` as b ON a.`SellerID`=b.`SellerID`";
		$sql .= " WHERE 1" . $append_where . $append_orderby . $append_limit;
        //echo $sql;

        $arr_sellerDiscs = array();

        $discs = $this->core->db->get_results($sql);
        if(!empty($discs)){
            foreach($discs as $disc){
                $arr_sellerDiscs[] = array(
                    'SellerDisID' => $disc->SellerDisID,
                    'SellerMsg' => $disc->SellerMsg,
                    'SellerMsgIs' => $disc->SellerMsgIs,
					'SellerNameCN' => $disc->SellerNameCN,
					'SellerMsgTime' => $disc->SellerMsgTime
                );
            }
        }

        return array('list' => $arr_sellerDiscs, 'pager' => $pager);
    }

    //获取商家分类列表
    function getSellerCateList($arr_filter = "", $arr_orderby = "", $arr_limit = array('page' => 1, 'pagesize' => 20, 'url' => '')){
        $arr_sql_appends = $this->buildAppendSql($arr_filter, $arr_orderby, $arr_limit);
        $append_where = $arr_sql_appends['where'];
        $append_orderby = $arr_sql_appends['order'];
        $append_limit = $arr_sql_appends['limit'];
        $append_page_where = $arr_sql_appends['page_where'];

        //分页处理
        if($arr_limit)
        $pager = $this->core->utility->pager('fun_seller_class', 'SellerClassID', $append_page_where, $arr_limit);


        $sql = "SELECT a.`SellerClassID`, a.`SellerClassName`, a.`SellerCreateMoney`, a.`SellerPicMoney`,
                a.`SellerGradeMoney`, a.`SellerMsgMoney`, b.`SellerClassName` as SellerClassPName";
		$sql .= " FROM `fun_seller_class` as a ";
		$sql .= " LEFT JOIN `fun_seller_class` as b ON a.`SellerClassOwner`=b.`SellerClassID`";
		$sql .= " WHERE 1" . $append_where . $append_orderby . $append_limit;
        //echo $sql;

        $arr_sellerCates = array();

        $cates = $this->core->db->get_results($sql);
        if(!empty($cates)){
            foreach($cates as $cate){
                $arr_sellerCates[] = array(
                    'SellerClassID' => $cate->SellerClassID,
                    'SellerClassName' => $cate->SellerClassName,
                    'SellerCreateMoney' => $cate->SellerCreateMoney,
                    'SellerPicMoney' => $cate->SellerPicMoney,
                    'SellerGradeMoney' => $cate->SellerGradeMoney,
                    'SellerMsgMoney' => $cate->SellerMsgMoney,
                    'SellerClassPName' => $cate->SellerClassPName,
                );
            }
        }

        return array('list' => $arr_sellerCates, 'pager' => $pager);
    }

    //获取省份简单列表
    function getSellerAllProvinceList(){
        $sql = "SELECT `SellerProvinceID`, `SellerProvinceChar`, `SellerProvinceNameCN` FROM `fun_seller_province` ORDER BY `SellerProvinceChar` ASC";
		$res = $this->core->db->get_results($sql);
		$arr_res = array();
		if($res){
			foreach($res as $province){
				$arr_res[] = array(
									'pid' => $province->SellerProvinceID,
									'pchar' => $province->SellerProvinceChar,
									'pname' => $province->SellerProvinceNameCN
								);
			}
		}
		return $arr_res;
    }

    //获取城市简单列表
    function getSellerAllCityList($pid = ""){
        if($pid) $append = " AND `SellerProvinceID`='".(int)$pid."'";
        $sql = "SELECT `SellerCityID`, `SellerCityNameCN`, `SellerCityNo` FROM `fun_seller_city`
                WHERE 1 ".$append." ORDER BY `SellerCityNo` ASC";
		$res = $this->core->db->get_results($sql);
		$arr_res = array();
		if($res){
			foreach($res as $province){
				$arr_res[] = array(
									'cid' => $province->SellerCityID,
									'cname' => $province->SellerCityNameCN,
									'cno' => $province->SellerCityNo
								);
			}
		}
		return $arr_res;
    }

    //获取商圈简单列表
    function getSellerAllCBDList($cid = ""){
        if($cid) $append = " AND `SellerCityID`='".(int)$cid."'";
        $sql = "SELECT `SellerCBDID`, `SellerCBDNameCN`, `SellerCityID` FROM `fun_seller_cbd`
                 WHERE 1 ".$append." ORDER BY `SellerCBDID` ASC";
		$res = $this->core->db->get_results($sql);
		$arr_res = array();
		if($res){
			foreach($res as $cbd){
				$arr_res[] = array(
									'cbdid' => $cbd->SellerCBDID,
									'cbdname' => $cbd->SellerCBDNameCN,
									'cityid' => $cbd->SellerCityID
								);
			}
		}
		return $arr_res;
    }

	//获取商家简单列表
	function getSellerAllList($sid = ""){
		if($sid) $append = " AND `SellerID`='".(int)$sid."'";
        $sql = "SELECT * FROM `fun_seller`
                 WHERE 1 ".$append." ORDER BY `SellerID` DESC";
		$res = $this->core->db->get_results($sql);
		$arr_res = array();
		if($res){
			foreach($res as $seller){
				$arr_res[] = array(
									'sellerid' => $seller->SellerID,
									'sellername' => $seller->SellerNameCN
								);
			}
		}
		return $arr_res;
	}

	//获取商家分类简单列表
	function getSellerCates($pid = 0, &$arr_cate="", &$level=0){
		$sql = "SELECT * FROM `fun_seller_class` WHERE `SellerClassOwner`='".$pid."'";
		$res = $this->core->db->get_results($sql);
		if($res){
			foreach($res as $cate){
				$arr_cate[] = array('SellerClassID'=>$cate->SellerClassID, 'SellerClassName'=>$cate->SellerClassName,
									'SellerClassOwner'=>$cate->SellerClassOwner, 'SellerCreateMoney'=>$cate->SellerCreateMoney,
									'SellerPicMoney'=>$cate->SellerPicMoney, 'SellerGradeMoney'=>$cate->SellerGradeMoney,
									'SellerMsgMoney'=>$cate->SellerMsgMoney, 'level'=>$level);
				$tag = $this->checkIsLeafCate($cate->SellerClassID);
				if(!$tag){
					$level++;
					$this->getSellerCates($cate->SellerClassID, &$arr_cate, $level);
				}else{
					$level--;
				}
			}
		}
		return;
	}

	//判断商家分类是否是叶子分类
	function checkIsLeafCate($cid){
		$sql = "SELECT count(*) FROM `fun_seller_class` WHERE `SellerClassOwner`='".$cid."'";
		$count = $this->core->db->get_var($sql);
		if($count > 0) return false;
			else return true;
	}

	//获取商家信息
	function getSellerInfo($seller_id){
		if(!$seller_id) return false;
        $sql = "SELECT * FROM `fun_seller` WHERE `SellerID`=" . $seller_id;
        $res = $this->core->db->get_row($sql);
        $arr_seller = array();
        if($res){

            foreach($res as $k => $v){
                $arr_seller[$k] = $v;
            }

        }
        return $arr_seller;
	}

    //获取省份信息
    function getSellerProvinceInfo($province_id){
        if(!$province_id) return false;
        $sql = "SELECT `SellerProvinceID`, `SellerProvinceChar`, `SellerProvinceNameCN` FROM `fun_seller_province` WHERE `SellerProvinceID`=" . $province_id;
        $res = $this->core->db->get_row($sql);
        $arr_province = array();
        if($res){
            $arr_province['SellerProvinceID'] = $res->SellerProvinceID;
            $arr_province['SellerProvinceChar'] = $res->SellerProvinceChar;
            $arr_province['SellerProvinceNameCN'] = $res->SellerProvinceNameCN;
        }
        return $arr_province;
    }

	//获取城市信息
    function getSellerCityInfo($city_id){
        if(!$city_id) return false;
        $sql = "SELECT `SellerCityID`, `SellerProvinceID`, `SellerCityNameCN`, `SellerCityNo` FROM `fun_seller_city` WHERE `SellerCityID`=" . $city_id;
        $res = $this->core->db->get_row($sql);
        $arr_city = array();
        if($res){
            $arr_city['SellerCityID'] = $res->SellerCityID;
            $arr_city['SellerProvinceID'] = $res->SellerProvinceID;
            $arr_city['SellerCityNameCN'] = $res->SellerCityNameCN;
			$arr_city['SellerCityNo'] = $res->SellerCityNo;
        }
        return $arr_city;
    }
    
    //获取商圈信息
    function getSellerCBDInfo($cbd_id){
        if(!$cbd_id) return false;
        $sql = "SELECT `SellerCBDID`, `SellerCBDNameCN`, `SellerCityID` FROM `fun_seller_cbd` WHERE `SellerCBDID`=" . $cbd_id;
        $res = $this->core->db->get_row($sql);
        $arr_cbd = array();
        if($res){
            $arr_cbd['SellerCBDID'] = $res->SellerCBDID;
            $arr_cbd['SellerCBDNameCN'] = $res->SellerCBDNameCN;
            $arr_cbd['SellerCityID'] = $res->SellerCityID;
        }
        return $arr_cbd;
    }

    //获取地区信息
    function getSellerAreaInfo($area_id){
        if(!$area_id) return false;
        $sql = "SELECT `SellerAreaID`, `SellerAreaNameCN`, `SellerCityID` FROM `fun_seller_area` WHERE `SellerAreaID`=" . $area_id;
        $res = $this->core->db->get_row($sql);
        $arr_area = array();
        if($res){
            $arr_area['SellerAreaID'] = $res->SellerAreaID;
            $arr_area['SellerAreaNameCN'] = $res->SellerAreaNameCN;
            $arr_area['SellerCityID'] = $res->SellerCityID;
        }
        return $arr_area;
    }

	//获取商家折扣信息
	function getSellerDiscountInfo($did){
		if(!$did) return false;
        $sql = "SELECT `SellerDisID`, `SellerID`, `SellerMsg`, `SellerMsgIs`, `SellerMsgTime` FROM `fun_seller_discount` WHERE `SellerDisID`=" . $did;
        $res = $this->core->db->get_row($sql);
        $arr_info = array();
        if($res){
            $arr_info['SellerDisID'] = $res->SellerDisID;
            $arr_info['SellerID'] = $res->SellerID;
            $arr_info['SellerMsg'] = $res->SellerMsg;
			$arr_info['SellerMsgIs'] = $res->SellerMsgIs;
			$arr_info['SellerMsgTime'] = $res->SellerMsgTime;
        }
        return $arr_info;
	}

    //获取分类信息
    function getSellerCateInfo($cate_id){
        if(!$cate_id) return false;
        $sql = "SELECT `SellerClassID`, `SellerClassName`, `SellerClassOwner`, `SellerCreateMoney`,
                `SellerPicMoney`, `SellerGradeMoney`, `SellerMsgMoney` FROM `fun_seller_class` 
                WHERE `SellerClassID`=" . $cate_id;
        $res = $this->core->db->get_row($sql);
        $arr_cate = array();
        if($res){
            $arr_cate['SellerClassID'] = $res->SellerClassID;
            $arr_cate['SellerClassName'] = $res->SellerClassName;
            $arr_cate['SellerClassOwner'] = $res->SellerClassOwner;
            $arr_cate['SellerCreateMoney'] = $res->SellerCreateMoney;
            $arr_cate['SellerPicMoney'] = $res->SellerPicMoney;
            $arr_cate['SellerGradeMoney'] = $res->SellerGradeMoney;
            $arr_cate['SellerMsgMoney'] = $res->SellerMsgMoney;
        }
        return $arr_cate;
    }

    //构建SQL查询语句
    function buildAppendSql_new($arr_join = "",$arr_filter = "", $arr_orderby = "", $arr_limit = array('page' => 1, 'pagesize' => 20, 'url' => '')){
        $append_where = "";
		$append_orderby = "";
		$append_limit = "";
        $append_page_where = "";
        $append_join = "";

        if(!empty($arr_join)){
            foreach($arr_join as $join){
                $append_join .= " LEFT JOIN `".$join['table']."` as ".$join['table_short']." ON ".$join['on'];
            }
        }

		if(!empty($arr_filter)){
			foreach($arr_filter as $filter){
                $join_append = "a";
                if($filter['join']) $join_append = $filter['join'];
				if($filter['match'] != 'like'){
                    if($filter['match'] == 'in'){
                        $append_where .= " AND ".$join_append.".`" . $filter['attr'] . "` IN (" . $filter['value'] . ")";
                        if($join_append == "a") $append_page_where .= " AND ".$join_append.".`" . $filter['attr'] . "` IN (" . $filter['value'] . ")";
                    }else{
                        $append_where .= " AND ".$join_append.".`" . $filter['attr'] . "` = '" . $filter['value'] . "'";
                        if($join_append == "a") $append_page_where .= " AND ".$join_append.".`" . $filter['attr'] . "` = '" . $filter['value'] . "'";
                    }
				}else{
					$append_where .= " AND ".$join_append.".`" . $filter['attr'] . "` LIKE '%" . $filter['value'] . "%'";
                    if($join_append == "a") $append_page_where .= " AND ".$join_append.".`" . $filter['attr'] . "` LIKE '%" . $filter['value'] . "%'";
				}
			}
		}

		if(!empty($arr_orderby)){
			foreach($arr_orderby as $attr => $orderby){
				$append_orderby .= " ORDER BY a.`" . $attr . "` " . $orderby;
			}
		}

		if(!empty($arr_limit)){
            $arr_limit['offset'] = ($arr_limit['page'] - 1) * $arr_limit['pagesize'];
            $append_limit .= " LIMIT " . $arr_limit['offset'] . "," . $arr_limit['pagesize'];
        }

        return array('where' => $append_where, 'page_where' => $append_page_where, 'order' => $append_orderby, 'limit' => $append_limit, 'join'=> $append_join);
    }

    //获取信息列表
    function getInfoList($table_info, $arr_join = "", $arr_filter = "", $arr_orderby = "", $arr_limit = array('page' => 1, 'pagesize' => 20, 'url' => '')){
        $arr_sql_appends = $this->buildAppendSql_new($arr_join, $arr_filter, $arr_orderby, $arr_limit);
        $append_where = $arr_sql_appends['where'];
        $append_orderby = $arr_sql_appends['order'];
        $append_limit = $arr_sql_appends['limit'];
        $append_page_where = $arr_sql_appends['page_where'];
        $append_join = $arr_sql_appends['join'];

        $table_name = $table_info['table'];
        $table_primary = $table_info['primary'];

        //分页处理
        if($arr_limit) $pager = $this->core->utility->pager($table_name, $table_primary, $append_page_where, $arr_limit);



        $sql = "SELECT * ";
		$sql .= " FROM `".$table_name."` as a ";
        $sql .= $append_join;
		$sql .= " WHERE 1" . $append_where . $append_orderby . $append_limit;
        //echo $sql;

        $arr_infolists = array();

        $lists = $this->core->db->get_results($sql);


        if(!empty($lists)){
            foreach($lists as $key => $list){
                foreach($list as $k => $v){
                    $arr_infolists[$key][$k] = $v;
                }
            }
        }

        return array('list' => $arr_infolists, 'pager' => $pager);
    }

    /***************添加操作***************/
    //添加省份
    function addNewProvince($province_name, $province_char){
        if(!$province_name || !$province_char){
            $this->error .= "没有输入省份名或首字母！";
            return false;
        }
        $sql = "SELECT `SellerProvinceID` FROM `fun_seller_province` WHERE `SellerProvinceNameCN`='".$province_name."' LIMIT 1";
        $tag = $this->core->db->get_var($sql);
        if($tag){
            $this->error .= "已经存在该省份！";
            return false;
        }
        $sql = "INSERT INTO `fun_seller_province` SET `SellerProvinceChar`='".$province_char."', `SellerProvinceNameCN`='".$province_name."'";
        $res = $this->core->db->query($sql);
        if(!$res){
            $this->error .= "添加新省份失败！";
        }
        return $res;
    }

	//添加城市
    function addNewCity($province_id, $city_name, $city_no){
        if(!$province_id || !$city_name || !$city_no){
            $this->error .= "没有选择省份或者没有填写城市资料！";
            return false;
        }
        $sql = "SELECT `SellerCityID` FROM `fun_seller_city` WHERE `SellerCityNameCN`='".$city_name."' LIMIT 1";
        $tag = $this->core->db->get_var($sql);
        if($tag){
            $this->error .= "已经存在该城市！";
            return false;
        }
        $sql = "INSERT INTO `fun_seller_city` SET `SellerProvinceID`='".$province_id."', `SellerCityNameCN`='".$city_name."', `SellerCityNo`=".$city_no;
        $res = $this->core->db->query($sql);
        if(!$res){
            $this->error .= "添加新城市失败！";
        }
        return $res;
    }

    //添加商圈
    function addNewCBD($city_id, $cbd_name){
        if(!$city_id || !$cbd_name){
            $this->error .= "没有选择城市或者没有填写商圈资料！";
            return false;
        }
        $sql = "SELECT `SellerCBDID` FROM `fun_seller_cbd` WHERE `SellerCBDNameCN`='".$cbd_name."' AND `SellerCityID`='".$city_id."' LIMIT 1";
        $tag = $this->core->db->get_var($sql);
        if($tag){
            $this->error .= "已经存在该商圈！";
            return false;
        }
        $sql = "INSERT INTO `fun_seller_cbd` SET `SellerCityID`='".$city_id."', `SellerCBDNameCN`='".$cbd_name."'";
        $res = $this->core->db->query($sql);
        if(!$res){
            $this->error .= "添加新商圈失败！";
        }
        return $res;
    }
    
    //添加地区
    function addNewArea($city_id, $area_name){
        if(!$city_id || !$area_name){
            $this->error .= "没有选择城市或者没有填写地区资料！";
            return false;
        }
        $sql = "SELECT `SellerAreaID` FROM `fun_seller_area` WHERE `SellerAreaNameCN`='".$area_name."' AND `SellerCityID`='".$city_id."' LIMIT 1";
        $tag = $this->core->db->get_var($sql);
        if($tag){
            $this->error .= "已经存在该地区！";
            return false;
        }
        $sql = "INSERT INTO `fun_seller_area` SET `SellerCityID`='".$city_id."', `SellerAreaNameCN`='".$area_name."'";
        $res = $this->core->db->query($sql);
        if(!$res){
            $this->error .= "添加新地区失败！";
        }
        return $res;
    }

	//添加商家分类
    function addNewCate($cate_pid, $cate_name, $cate_cm, $cate_pm, $cate_gm, $cate_mm){
        if(!$cate_name){
            $this->error .= "没有填写分类名！";
            return false;
        }
        $sql = "SELECT `SellerClassID` FROM `fun_seller_class` WHERE `SellerClassName`='".$cate_name."' AND `SellerClassOwner`='".$cate_pid."' LIMIT 1";
        $tag = $this->core->db->get_var($sql);
        if($tag){
            $this->error .= "已经存在该分类！";
            return false;
        }
        $sql = "INSERT INTO `fun_seller_class` SET `SellerClassName`='".$cate_name."', `SellerClassOwner`='".$cate_pid."', `SellerCreateMoney`='".$cate_cm."'
				, `SellerPicMoney`='".$cate_pm."', `SellerGradeMoney`='".$cate_gm."', `SellerMsgMoney`='".$cate_mm."'";
        $res = $this->core->db->query($sql);
        if(!$res){
            $this->error .= "添加新分类失败！";
        }
        return $res;
    }

	//添加新商家
	function addNewSeller($seller){
		if(!$seller){
            $this->error .= "参数不合法，请检查！";
            return false;
        }

		$arr_append_sql = array();
		$append_sql = "";
        $append_check = "";
		foreach($seller as $attr => $value){
			$arr_append_sql[] = " `".$attr."` = '".$value."'";
            $append_check .= " AND `".$attr."` = '".$value."'";
		}
		$append_sql = @implode(',', $arr_append_sql);

        $sql = "SELECT count(*) FROM `fun_seller` WHERE 1" . $append_check;
        $tag = $this->core->db->get_var($sql);
        if($tag){
            $this->error .= "已经存在该商家，添加失败！";
            return false;
        }

		$sql = "INSERT INTO `fun_seller` SET ".$append_sql." , `SellerCreatTime`=now()";
		$res = $this->core->db->query($sql);
		return $res;
	}

	//添加新商家折扣
	function addNewSellerDiscount($info){
		if(!$info){
            $this->error .= "参数不合法，请检查！";
            return false;
        }

		$arr_append_sql = array();
		$append_sql = "";
        $append_check = "";
		foreach($info as $attr => $value){
			$arr_append_sql[] = " `".$attr."` = '".$value."'";
            $append_check .= " AND `".$attr."` = '".$value."'";
		}
		$append_sql = @implode(',', $arr_append_sql);

        $sql = "SELECT count(*) FROM `fun_seller_discount` WHERE 1" . $append_check;
        $tag = $this->core->db->get_var($sql);
        if($tag){
            $this->error .= "已经存在该商家折扣，添加失败！";
            return false;
        }

		$sql = "INSERT INTO `fun_seller_discount` SET ".$append_sql." , `SellerMsgTime`=now()";
		$res = $this->core->db->query($sql);
		return $res;
	}

	/***************更新操作***************/
	//更新省份
	function updateProvince($province_id, $province_info){
		if(!$province_id || !$province_info){
            $this->error .= "参数不合法，请检查！";
            return false;
        }

		$province_char = $province_info['char'];
		$province_name = $province_info['name'];
		$sql = "UPDATE `fun_seller_province` SET `SellerProvinceChar`='".$province_char."', `SellerProvinceNameCN`='".$province_name."' WHERE `SellerProvinceID`=" . $province_id;
		$res = $this->core->db->query($sql);
		return $res;
	}

	//更新城市/省份/地区
	function updateInfo($update_table, $update_primary , $update_id, $update_Info){
		if(!$update_table || !$update_id || !$update_Info){
            $this->error .= "参数不合法，请检查！";
            return false;
        }

		$arr_append_update = array();
		$append_update = "";
        $append_check = "";
		foreach($update_Info as $attr => $value){
			$arr_append_update[] = " `".$attr."` = '".$value."'";
            $append_check .= " AND `".$attr."` = '".$value."'";
		}
		$append_update = @implode(',', $arr_append_update);

        $sql = "SELECT count(*) FROM `".$update_table."` WHERE 1" . $append_check . " AND `".$update_primary."` != '".$update_id."'";
        $tag = $this->core->db->get_var($sql);
        if($tag){
            $this->error .= "已经存在该值，更新失败！";
            return false;
        }
		
		$sql = "UPDATE `".$update_table."` SET ".$append_update." WHERE `".$update_primary."`='" . $update_id ."'";
		$res = $this->core->db->query($sql);
		return $res;
	}

	/***************删除操作***************/
	//删除省份
	function delProvince($province_id){
		if(!$province_id){
            $this->error .= "请选择要删除的省份！";
            return false;
        }

		$sql = "DELETE FROM `fun_seller_province` WHERE `SellerProvinceID`=" . $province_id;
		$res = $this->core->db->query($sql);
		return $res;
	}

	//删除省份、城市、地区、分类
	function delInfo($del_table, $del_primary, $del_id){
		if(!$del_table || !$del_primary || !$del_id){
            $this->error .= "请选择要删除的选项！";
            return false;
        }

		$sql = "DELETE FROM `".$del_table."` WHERE `".$del_primary."`=" . $del_id;
		$res = $this->core->db->query($sql);
		return $res;
	}
}
?>
